﻿using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
#endif 

/// <summary>
/// 让枚举在Inspector面板中显示中文别名。放到项目里即可使用。
/// 
/// 示例：
/** 
public enum Example
{
	[EnumLabel("高")]
	HIGH,
	[EnumLabel("低")]
	LOW
}
[EnumLabel("例子")]
public Example test = Example.HIGH;
*/
/// </summary>
	
	[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
	public class EnumLabelAttribute : PropertyAttribute
	{
		public string label;
		public EnumLabelAttribute(string label)
		{
			this.label = label;
		}
	}


	#if UNITY_EDITOR
	[CustomPropertyDrawer(typeof(EnumLabelAttribute))]
	public class EnumLabelDrawer : PropertyDrawer
	{
		private Dictionary<string, string> customEnumNames = new Dictionary<string, string>();

		public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
		{
			SetUpCustomEnumNames(property, property.enumNames);

			if (property.propertyType == SerializedPropertyType.Enum)
			{
				EditorGUI.BeginChangeCheck();
				string[] displayedOptions = property.enumNames
					.Where(enumName => customEnumNames.ContainsKey(enumName))
					.Select<string, string>(enumName => customEnumNames[enumName])
					.ToArray();
				int selectedIndex = EditorGUI.Popup(position, enumLabelAttribute.label, property.enumValueIndex, displayedOptions);
				if (EditorGUI.EndChangeCheck())
				{
					property.enumValueIndex = selectedIndex;
				}
			}
		}

		private EnumLabelAttribute enumLabelAttribute
		{
			get
			{
				return (EnumLabelAttribute)attribute;
			}
		}

		public void SetUpCustomEnumNames(SerializedProperty property, string[] enumNames)
		{
			Type type = property.serializedObject.targetObject.GetType();
			foreach (FieldInfo fieldInfo in type.GetFields())
			{
				object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(EnumLabelAttribute), false);
				foreach (EnumLabelAttribute customAttribute in customAttributes)
				{
					Type enumType = fieldInfo.FieldType;

					foreach (string enumName in enumNames)
					{
						FieldInfo field = enumType.GetField(enumName);
						if (field == null) continue;
						EnumLabelAttribute[] attrs = (EnumLabelAttribute[])field.GetCustomAttributes(customAttribute.GetType(), false);

						if (!customEnumNames.ContainsKey(enumName))
						{
							foreach (EnumLabelAttribute labelAttribute in attrs)
							{
								customEnumNames.Add(enumName, labelAttribute.label);
							}
						}
					}
				}
			}
		}
	}

	#endif
